package com.xiaoyuzhou.adapter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.xiaoyuzhou.R;
import com.xiaoyuzhou.utils.CalendarUtils;

import android.content.Context;

public class DataAdapter {
	
	public static final String RAIN = "雨";
	public static final String SNOW = "雪";
	public static final String FINE = "晴";
	public static final String CLOUDY = "多云";
	
	public static final String NIGHT_RAIN = "night雨";
	public static final String NIGHT_SNOW = "night雪";
	public static final String NIGHT_FINE = "night晴";
	public static final String NIGHT_CLOUDY = "night多云";
	
	
	
	public static InputStream getStreamFromAssets(Context context, String fileName) throws Exception {
		return context.getAssets().open(fileName);
	}
	
	public static String getWeaIconName(Context context, String pWeather) throws Exception {
		if(pWeather == null || pWeather.equals("")) {
			return null;
		}
		String _wea = pWeather.substring(0, pWeather.indexOf("转"));
		if(CalendarUtils.isNight()) {//如果是夜晚，需要做特殊处理，晚上只有四种天气图标
			if (_wea.contains(RAIN)) {//雨
				_wea = NIGHT_RAIN;
			} else if(_wea.contains(SNOW)) {//雪
				_wea = NIGHT_SNOW;
			} else if(_wea.contains(NIGHT_FINE)){//晴
				_wea = NIGHT_FINE;
			} else {//其他都按多云处理
				_wea = NIGHT_CLOUDY;
			}
		}
		InputStream is = getStreamFromAssets(context, "wea_icon.prop");
		BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
		String line = "";
		while((line = reader.readLine()) != null) {
			String _s[] = line.trim().split("=");
			String weather = _s[1];
			String icon = _s[0];
			if(weather.equals(_wea)) {
				return icon;
			} 
		}
		return null;
	}
	
	public static int getWeaIconId(Context context, String pWeather) throws Exception {
		String icon = getWeaIconName(context, pWeather);
		int id = context.getResources().getIdentifier(icon, "drawable", context.getPackageName());
		return id;
	}
	
}
